package Java多线程和并发.CAS和Unsafe和原子类详解;

import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2022-02-20 12:36
 **/
public class TestAtomicIntegerFieldUpdater {

    public static void main(String[] args) {
        TestAtomicIntegerFieldUpdater tester = new TestAtomicIntegerFieldUpdater();
        tester.doIt();
    }

    public AtomicIntegerFieldUpdater<DateDemo> updater(String fieldName) {
        return AtomicIntegerFieldUpdater.newUpdater(DateDemo.class, fieldName);
    }

    public void doIt() {

        DateDemo dateDemo = new DateDemo();

        System.out.println("publicVar = " + updater("publicVar").getAndAdd(dateDemo, 2));

        System.out.println("protectedVar = " + updater("protectedVar").getAndAdd(dateDemo, 2));

        System.out.println("privateVar = " + updater("privateVar").getAndAdd(dateDemo, 2));

    }
}

